package com.skhu.as.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.skhu.as.domain.DutyHours;
import com.skhu.as.domain.FaultHandling;
import com.skhu.as.domain.FaultHandlingDomain;
import com.skhu.as.domain.PresentCondition;
import com.skhu.as.domain.PresentConditionDomain;
import com.skhu.as.domain.Worker;
import com.skhu.as.service.IndexS;
import com.skhu.as.util.DateU;

public class IndexC extends SimpleFormController {

	private IndexS indexS;
	
	public void setIndexS(IndexS indexS) {
		this.indexS = indexS;
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		
		return super.formBackingObject(request);
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		
		DateU dateU = new DateU();
		String dateStr = dateU.getDate();
		Map ref = new HashMap();
		
		String year = ServletRequestUtils.getStringParameter(request, "year", "");
		String month = ServletRequestUtils.getStringParameter(request, "month", "");
		String dayOfMonth = ServletRequestUtils.getStringParameter(request, "dayOfMonth", "");
		String faultHandlingNo = ServletRequestUtils.getStringParameter(request, "faultHandlingNo", "");
		
		if(!year.equals("")){
			if(month.length() == 1){
				month = "0" + month;
			}
			if(dayOfMonth.length() == 1){
				dayOfMonth = "0" + dayOfMonth;
			}
			dateStr = year + "" + month + "" + dayOfMonth;
		}
		
		ref.put("year", year);
		ref.put("month", month);
		ref.put("dayOfMonth", dayOfMonth);
		
		List<FaultHandling> untreatedWorkList = indexS.getUntreatedWork();
		ref.put("untreatedWorkList", untreatedWorkList);
		ref.put("size", untreatedWorkList.size());
		
		List<DutyHours> dutyHoursList = indexS.getDutyHoursList();
		ref.put("dutyHoursList", dutyHoursList);
		
		List<FaultHandlingDomain> faultHandlingList = indexS.getFaultHandlingDomainByDate(dateStr);
		int faultHandlingListSize = faultHandlingList.size();
		ref.put("faultHandlingList", faultHandlingList);
		
		List<FaultHandlingDomain> faultHandling = indexS.getFaultHandlingDomainByNo(dateStr, faultHandlingNo);
		if(faultHandlingNo == ""){
			if(faultHandlingList.size() != 0){
				ref.put("faultHandling", faultHandlingList.get(0));
				faultHandlingNo = String.valueOf(faultHandlingList.get(0).getFaultHandlingNo());
			}
		}else{
			ref.put("faultHandling", faultHandling.get(0));
		}
		
		
		int[] faultHandlingNoList = new int[faultHandlingListSize];
		
		for(int i=0; i<faultHandlingListSize; i++){
			faultHandlingNoList[i] = faultHandlingList.get(i).getFaultHandlingNo();
		}
		
		List<Worker> workerList = indexS.getWorkerList();
		ref.put("workerList", workerList);
		
		List<PresentConditionDomain> presentConditionList = indexS.getPresentConditionList(faultHandlingNo); 
		ref.put("presentConditionList", presentConditionList);
		
		return ref;
	}

}
